/*global $ */
import Vue from 'vue'

// Register a global custom directive called v-focus
/*
* 用于绑定Componet的日期
* v-date="start_at" 绑定日期到start_at
*/
Vue.directive('date', {
  bind (el, binding, vnode, oldValue) {
    let $el = $(el)
    $el.change(function () {
      // console.log('input date changed ', $(this).val())
      vnode.context[binding.expression] = $(this).val()
    })
  },
  update (el, binding, vnode, oldValue) {
    // console.log('directive date update ->', binding)
    $(el).val(vnode.context[binding.expression])
  },
  inserted (el, binding, vnode, oldValue) {
    // 需要监听清除日期按钮
    let $el = $(el)
    let datePickerBindedElement = $el
    if ($el.parent().hasClass('input-group')) {
      // console.log('binding event to parent')
      datePickerBindedElement = $el.parent()
    }
    datePickerBindedElement.on('clearDate', function () {
      vnode.context[binding.expression] = null
    })
  }
})
